clear 

#delimit ;

**************************************************************************
   Label           : fim10243_gid_BA_2_BAL_wide
   Rows            : 68382
   Columns         : 10
   ASCII File Date : April 27, 2020
*************************************************************************;


infix 
         ER30001               1 - 4    
         ER30002               5 - 7    
         ER30001_P_AF          8 - 11   
         ER30002_P_AF         12 - 14   
         ER30001_P_AM         15 - 18   
         ER30002_P_AM         19 - 21   
         ER30001_P_F          22 - 25   
         ER30002_P_F          26 - 28   
         ER30001_P_M          29 - 32   
         ER30002_P_M          33 - 35   
using ${rawdata}/fim10243_gid_BA_2_BAL_wide.txt, clear 
;
label variable  ER30001              "1968 INTERVIEW NUMBER" ;
label variable  ER30002              "PERSON NUMBER 68" ;
label variable  ER30001_P_AF         "1968 INTERVIEW NUMBER /PARENT /ADOPTIVE FATHER" ;
label variable  ER30002_P_AF         "PERSON NUMBER 68 /PARENT /ADOPTIVE FATHER" ;
label variable  ER30001_P_AM         "1968 INTERVIEW NUMBER /PARENT /ADOPTIVE MOTHER" ;
label variable  ER30002_P_AM         "PERSON NUMBER 68 /PARENT /ADOPTIVE MOTHER" ;
label variable  ER30001_P_F          "1968 INTERVIEW NUMBER /PARENT /FATHER" ;
label variable  ER30002_P_F          "PERSON NUMBER 68 /PARENT /FATHER" ;
label variable  ER30001_P_M          "1968 INTERVIEW NUMBER /PARENT /MOTHER" ;
label variable  ER30002_P_M          "PERSON NUMBER 68 /PARENT /MOTHER" ;


#delimit cr

* Generate new unique identifiers

*We create a unique identifier for sons in the sons dataset
gen son_id = ER30001*1000+ER30002

*In the FIMS file, we also create an identifier for dads (not moms)

gen dad_id = .
replace dad_id = ER30001_P_AF*1000 + ER30002_P_AF if ER30001_P_AF != .
replace dad_id = ER30001_P_F*1000 + ER30002_P_F if ER30001_P_F != .
replace dad_id = . if ER30001_P_F == . & ER30001_P_AF == .

*And now we create a dad id for mums who are heads of the household 

gen mom_id = .
replace mom_id = ER30001_P_AM*1000 + ER30002_P_AM if ER30001_P_AM != .
replace mom_id = ER30001_P_M*1000 + ER30002_P_M if ER30001_P_M != .
replace mom_id = . if ER30001_P_M == . & ER30001_P_AM == .

*in both files (FIMS and MAIN DATA), data have to be ordered before merging
sort son_id

keep son_id dad_id mom_id

drop if dad_id==. & mom_id==.

save Data/ig_links_nocds_ad.dta, replace
